/* @(#)FlywheelStoryEditorFragment.java
 ** 
 ** Copyright (C) 2012 by Steven D. Stamps
 **
 **             Trademarks & Copyrights
 ** Flywheel Management Science(TM), Flywheel Management Model(TM),
 ** Flywheel Story Editor(TM) and FlywheelMS(TM) are exclusive trademarks
 ** of Steven D. Stamps and may only be used freely for the purpose of
 ** identifying the unforked version of this software.  Subsequent forks
 ** may not use these trademarks.  All other rights are reserved.
 **
 ** DecKanGL (Decorated Kanban Glyph Language) and TribKn (Tribal Knowledge)
 ** are also exclusive trademarks of Steven D. Stamps.  These may be used
 ** freely within the unforked FlywheelMS application and documentation.
 ** All other rights are reserved.
 **
 ** gConGUI (game Controller Graphical User Interface) is an exclusive
 ** trademark of Steven D. Stamps.  This trademark may be used freely
 ** within the unforked FlywheelMS application and documentation.
 ** All other rights are reserved.
 **
 ** Trademark information is available at
 ** <http://www.flywheelms.com/trademarks>
 **
 ** Flywheel Management Science(TM) is a copyrighted body of management
 ** metaphors, governance processes, and leadership techniques that is
 ** owned by Steven D. Stamps.  These copyrighted materials may be freely
 ** used, without alteration, by the community (users and developers)
 ** surrounding this GPL3-licensed software.  Additional copyright
 ** information is available at <http://www.flywheelms.org/copyrights>
 **
 **              GPL3 Software License
 ** This program is free software: you can use it, redistribute it and/or
 ** modify it under the terms of the GNU General Public License, version 3,
 ** as published by the Free Software Foundation. This program is distributed
 ** in the hope that it will be useful, but WITHOUT ANY WARRANTY; without
 ** even the implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR
 ** PURPOSE.  See the GNU General Public License for more details. You should
 ** have received a copy of the GNU General Public License, in a file named
 ** COPYING, along with this program.  If you cannot find your copy, see
 ** <http://www.gnu.org/licenses/gpl-3.0.html>.
 */
package com.flywheelms.library.fse.fragment;

import java.util.Date;

import android.app.Fragment;
import android.os.Bundle;
import android.view.LayoutInflater;
import android.view.View;
import android.view.View.OnClickListener;
import android.view.ViewGroup;
import android.widget.Button;
import android.widget.FrameLayout;
import android.widget.LinearLayout;

import com.flywheelms.library.FmsLibraryActivity;
import com.flywheelms.library.R;
import com.flywheelms.library.fmm.serialization.FseDocumentSerialization;
import com.flywheelms.library.fse.enumerator.FseContentModificationState;
import com.flywheelms.library.fse.interfaces.FseHostActivity;
import com.flywheelms.library.fse.interfaces.FseHostActivityMediator;
import com.flywheelms.library.fse.model.FseDocument;
import com.flywheelms.library.fse.model.FseDocumentTransactionType;
import com.flywheelms.library.fse.views.FseDocumentView;
import com.flywheelms.library.gcg.helper.GcgHelper;
import com.flywheelms.library.gcg.widget.GcgSpinnableMenu;

public class FlywheelStoryEditorFragment extends Fragment {
	
	public static final int RESOURCE_ID = 0x7f0a000c;
	private FseHostActivityMediator fseHostActivityMediator;
	private FrameLayout fseFragmentLayout;
	private FseDocumentView documentView;
	// START - data members that are NULL in portrait mode
	private FrameLayout leftMenuInclude;
	private LinearLayout leftMenuLayout;
//	private FrameLayout leftFdkKeypadParent;
	private LinearLayout leftFdkKeypad;
	private GcgSpinnableMenu leftSpinnableMenu;
	private Button saveDocumentButton;
	private Button newDocumentButton;
	private Button storageRevertDocumentButton;
	
	/**
	 * purpose:
	 * - parent view for the left menu, document view, right menu, and dual-touch shift widget
	 * - mediator for the left menu, document view, right menu, and dual-touch shift widget
	 */
	
	@Override
	public void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
//		this.setRetainInstance(true);
	}

	@Override
	public View onCreateView(LayoutInflater anInflater, ViewGroup aViewGroup,
			Bundle aSavedInstanceState) {
		this.fseFragmentLayout = (FrameLayout) anInflater.inflate(
				R.layout.fse_fragment, aViewGroup, false);
		initGuiElements();
		this.fseHostActivityMediator = ((FseHostActivity) getActivity()).getFseHostActivityMediator();
		String theSavedDocument = null;
		String theDocumentBaseline = null;
		if(aSavedInstanceState != null) {
			theSavedDocument = aSavedInstanceState.getString(FseDocumentSerialization.key__DOCUMENT_BODY);
			theDocumentBaseline = aSavedInstanceState.getString(FseDocumentSerialization.key__DOCUMENT_BASELINE);
		}
		if(theSavedDocument == null) {
			viewDocument(this.fseHostActivityMediator.getInitialDocument());
		} else {
			FseDocument theFseDocument = new FseDocument(theSavedDocument);
			if(theDocumentBaseline != null) {
				viewDocument(theFseDocument, new FseDocument(theDocumentBaseline));
			} else {
				viewDocument(theFseDocument);
			}
		}
		return this.fseFragmentLayout;
	}
	
	@Override
	public void onResume() {
		super.onResume();
	}
	
	@Override
	public void onActivityCreated(Bundle savedInstanceState) {
		  super.onActivityCreated(savedInstanceState);
	}


	@Override
	public void onSaveInstanceState(Bundle theBundle) {
		theBundle.putString(FseDocumentSerialization.key__DOCUMENT_BODY, this.documentView.getSerialized());
		theBundle.putString(FseDocumentSerialization.key__DOCUMENT_BASELINE, this.documentView.getBaselineDocument().getSerialized());
		super.onSaveInstanceState(theBundle);
	}

	private void viewDocument(FseDocument anFseDocument) {
		viewDocument(anFseDocument, anFseDocument);
	}

	private void viewDocument(FseDocument anFseDocument, FseDocument anFseDocumentBaseline) {
		// use fseHostActivityMediator to save any existing, dirty document currently displayed
		this.documentView.viewDocument(anFseDocument, anFseDocumentBaseline);
	}
	
	private void saveDocumentIfModified() {
		if(this.documentView != null) {
			String theSerializedFseDocument = this.documentView.getSerialized();
			if(theSerializedFseDocument != null) {
				this.fseHostActivityMediator.writeSerializedDocument(theSerializedFseDocument);
			}
		}
	}

	public void saveDocument() {
		GcgHelper.makeToast("Saving document...");
		FseContentModificationState theDocumentModificationState = this.documentView.getModificationState();
		FseDocumentTransactionType theDocumentTransactionType = null;
		if(theDocumentModificationState == FseContentModificationState.UNCHANGED) {
			return;
		}
		if(theDocumentModificationState == FseContentModificationState.NEW) {
			theDocumentTransactionType = FseDocumentTransactionType.CREATE;
		} else if(theDocumentModificationState == FseContentModificationState.MODIFIED) {
			theDocumentTransactionType = FseDocumentTransactionType.MODIFY;
		} else {
			theDocumentTransactionType = FseDocumentTransactionType.CHECKPOINT;
		}
		Date theTimestamp = new Date();
		this.getDocumentView().updateHistory(theTimestamp, theDocumentTransactionType);
		this.getDocumentView().updateAuditBlock(theTimestamp, theDocumentTransactionType);
		this.fseHostActivityMediator.writeSerializedDocument(this.documentView.getSerialized());
		this.getDocumentView().postPersistenceProcessing();
	}

	public void newDocument() {
		GcgHelper.makeToast("Creating new document...");
		viewDocument(this.fseHostActivityMediator.getInitialDocument());
	}
	
	public FseHostActivityMediator getFseHostActivityMediator() {
		return this.fseHostActivityMediator;
	}

	private void initGuiElements() {
		if(GcgHelper.isLandscapeMode()) {
			initLeftMenu();
			initFdkLeftKeypad();
			initDocumentView();
//			initRightMenu();
//			initMultiShiftWidget();
//			configMultiShiftClientButtons();
//			configMultiShiftStateListeners();
		} else {
			initDocumentView();
//			configMultiShiftStateListeners();
		}
	}

	private void initLeftMenu() {
		int[] theMenuBodyResourceIdArray = {
				R.id.fse__storage_menu__body,
				R.id.fse__share_menu__body,
				R.id.fse__import_menu__body };
//		this.leftWindowPane =
//				(RelativeLayout) this.documentView.getDocumentViewLayout().findViewById(getDocumentSectionViewResourceId()).findViewById(R.id.fse__right_window_pane);
		this.leftMenuInclude = (FrameLayout) LayoutInflater.from(getActivity()).inflate(R.layout.fse__left_menu__include, null);
		this.leftMenuLayout = (LinearLayout) this.leftMenuInclude.findViewById(R.id.fse__left_menu_layout);
//		this.leftFrameLayout = (FrameLayout) LayoutInflater.from(getActivity()).inflate(R.layout.fse__left_menu__include, null);
//		LinearLayout theLeftMenuLayout = (LinearLayout) this.leftFrameLayout.findViewById(R.id.fse__left_menu_layout);
		this.leftSpinnableMenu = new GcgSpinnableMenu(
				getActivity(),
				this.leftMenuLayout,
				GcgSpinnableMenu.DECORATORS_LEFT,
				R.id.fse__left_menu__heading_spinner,
				R.array.fse__left_menu__heading_array,
				theMenuBodyResourceIdArray );
		initStorageMenu();
		initShareMenu();
		initImportMenu();
	}
	
	private void initFdkLeftKeypad() {
//		this.leftFdkKeypadParent = this.leftMenuInclude;
//		this.leftFdkKeypad = (LinearLayout) this.leftFdkKeypadParent.findViewById(R.id.fdk__left_keypad);
		this.leftFdkKeypad = (LinearLayout) this.leftMenuInclude.findViewById(R.id.fdk__left_keypad__include);
		
	}
	
	public LinearLayout getLeftFdkKeypad() {
		return this.leftFdkKeypad;
	}
	
	public GcgSpinnableMenu getLeftSpinnableMenu() {
		return this.leftSpinnableMenu;
	}
	
	public FrameLayout getLeftMenuInclude() {
		return this.leftMenuInclude;
	}

	private void initStorageMenu() {
		this.saveDocumentButton = (Button) this.leftSpinnableMenu.getMenuFrameLayout().findViewById(R.id.storage__save_document__button);
		this.saveDocumentButton.setOnClickListener(new OnClickListener() {
			
			@Override
			public void onClick(View v) {
				FlywheelStoryEditorFragment.this.saveDocument();
			}
		});
		this.newDocumentButton = (Button) this.leftSpinnableMenu.getMenuFrameLayout().findViewById(R.id.storage__new_document__button);
		this.newDocumentButton.setOnClickListener(new OnClickListener() {
			
			@Override
			public void onClick(View v) {
				FlywheelStoryEditorFragment.this.newDocument();
			}
		});
		
		this.storageRevertDocumentButton = (Button) this.leftSpinnableMenu.getMenuFrameLayout().findViewById(R.id.storage__revert_document__button);
		this.storageRevertDocumentButton.setOnClickListener(new OnClickListener() {
			
			@Override
			public void onClick(View v) {
				FlywheelStoryEditorFragment.this.getDocumentView().revertDocument();
			}
		});
	}

	private void initShareMenu() {
		// TODO Auto-generated method stub
		
	}
	
	private void initImportMenu() {
		// TODO Auto-generated method stub
		
	}
	
	protected FseDocumentView getDocumentView() {
		return this.documentView;
	}

	private void initDocumentView() {
		this.documentView = ((FseDocumentView)
				this.fseFragmentLayout.findViewById(R.id.fse__document_view) );
		this.documentView.initialize(this);
	}

	public View getLeftMenuLayout() {
		return this.leftMenuLayout;
	}

}
